import { PetExplorePetItem } from "./PetExplorePetItem";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { DragEvent } from "../../../../../com/mui/events/DragEvent";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { Cache } from "../../../cache/Cache";
import { PetUtil } from "../info/PetUtil";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
//class PetExploreBattleListPanel
    
    export  class PetExploreBattleListPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._battlePetItemArray = [];
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            while (loc3 < 5) 
            {
                loc1 = new PetExplorePetItem();
                loc1.setSize(38, 38);
                ObjCreate.createBitmap(ImagesConst.PetCircleBg, loc3 * 60, 0, this);
                loc1.x = 5 + loc3 * 60;
                loc1.y = 3;
                loc1.index = loc3;
                loc1.isDragAble = true;
                loc1.isDropAble = true;
                loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onBattlePetItemClickHandler);
                loc1.addEventListener(DragEvent.Event_Move_In, this.onMoveInBattleHandler);
                this._battlePetItemArray.push(loc1);
                this.addChild(loc1);
                ++loc3;
            }
            return;
        }

        /* internal  */onBattlePetItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.clickFun != null) 
            {
                this.clickFun(arg1);
            }
            return;
        }

        /* internal  */onMoveInBattleHandler(arg1: DragEvent): void
        {
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc8=0;
            var loc9=null;
            var loc1=arg1.dragItem as PetExplorePetItem;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=arg1.dropItem as PetExplorePetItem;
            var loc3=loc1.index;
            var loc4=loc2.index;
            if (loc1.petInfo) 
            {
                if (loc2.petInfo != null) 
                {
                    if (loc4 == 4) 
                    {
                        loc5 = loc1.petInfo;
                        loc8 = loc3;
                        while (loc8 < this._battlePetItemArray.length) 
                        {
                            loc6 = this._battlePetItemArray[loc8] as PetExplorePetItem;
                            if (loc8 != (this._battlePetItemArray.length - 1)) 
                            {
                                loc7 = this._battlePetItemArray[loc8 + 1] as PetExplorePetItem;
                            }
                            else if (loc7) 
                            {
                                loc7.petInfo = loc5;
                            }
                            if (loc7) 
                            {
                                loc6.petInfo = loc7.petInfo;
                            }
                            ++loc8;
                        }
                    }
                    else 
                    {
                        loc5 = loc2.petInfo;
                        loc2.petInfo = loc1.petInfo;
                        loc1.petInfo = loc5;
                    }
                }
                else 
                {
                    loc2.petInfo = loc1.petInfo;
                    loc1.petInfo = null;
                }
                if (this.isSaveChange) 
                {
                    loc9 = this.getBattlePetUids();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreSetOrder, {"uids": loc9, "isShowTip": true}));
                }
            }
            return;
        }

        public updateByPetUids(arg1: Array<any>, arg2: Boolean=true): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=0;
            if (arg2) 
            {
                loc5 = 0;
                while (loc5 < this._battlePetItemArray.length) 
                {
                    if ((loc2 = this._battlePetItemArray[loc5] as PetExplorePetItem).petInfo) 
                    {
                        loc1 = loc2.petInfo.uid;
                        loc2.petInfo = Cache.instance.pet.getPetInfoByUid(loc1);
                    }
                    ++loc5;
                }
            }
            else 
            {
                this.clear();
                loc3 = Cache.instance.pet.getPetsByJudge(PetUtil.isCanExplore);
                loc4 = 0;
                while (loc4 < arg1.length) 
                {
                    loc1 = arg1[loc4] as string;
                    (loc2 = this._battlePetItemArray[loc4] as PetExplorePetItem).petInfo = Cache.instance.pet.getPetInfoByUid(loc1);
                    if (this.isPetInCanExploreList(loc1, loc3) == false) 
                    {
                        loc2.petInfo = null;
                    }
                    ++loc4;
                }
            }
            return;
        }

        public getBlankBattlePetItem(): PetExplorePetItem
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._battlePetItemArray.length) 
            {
                loc1 = this._battlePetItemArray[loc2] as PetExplorePetItem;
                if (loc1.petInfo == null) 
                {
                    return loc1;
                }
                ++loc2;
            }
            return null;
        }

        public getBattlePetUids(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            while (loc3 < this._battlePetItemArray.length) 
            {
                loc2 = this._battlePetItemArray[loc3] as PetExplorePetItem;
                if (loc2.petInfo) 
                {
                    loc1.push(loc2.petInfo.uid);
                }
                ++loc3;
            }
            return loc1;
        }

        /* internal  */isPetInCanExploreList(arg1: string, arg2: Array<any>): Boolean
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < arg2.length) 
            {
                loc1 = arg2[loc2] as SPetInfo;
                if (arg1 == loc1.uid) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        /* internal  */clear(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._battlePetItemArray.length) 
            {
                loc1 = this._battlePetItemArray[loc2] as PetExplorePetItem;
                loc1.petInfo = null;
                ++loc2;
            }
            return;
        }

        public /* var */clickFun: Function;

        public /* var */isSaveChange: Boolean;

        /* internal */ /* var */_battlePetItemArray: Array<any>;
    }
